<?php
    $display_published_at = $display_published_at ?? true;
    if ($display_published_at && !$link->published_at) {
        $display_published_at = false;
    }
    $display_notes = $display_notes ?? false;
    $display_external_comments = $display_external_comments ?? false;
    if ($display_external_comments && !$link->url_replies) {
        $display_external_comments = false;
    }
    $display_hidden = $display_hidden ?? false;
    $display_edit = $display_edit ?? false;
    $display_repair = $display_repair ?? false;
    $display_delete = $display_delete ?? false;
    $display_read_later = $display_read_later ?? false;
    $display_mark_as_read = $display_mark_as_read ?? false;
    $display_mark_as_unread = $display_mark_as_unread ?? false;
    $display_never = $display_never ?? false;
    $display_source = $display_source ?? false;
    if ($display_source && !$link->source_type) {
        $display_source = false;
    }
    $storing_must_mark_as_read = $storing_must_mark_as_read ?? false;

    $source_collection = $display_source ? $link->sourceCollection() : null;
    $source_user = $display_source ? $link->sourceUser() : null;

    $is_current_user_owner = $current_user && $current_user->id === $link->user_id;
    $in_error = $link->inError();
    $trackers_detected = $link->trackersDetected();

    $in_bookmarks = $link->isInBookmarksOf($current_user);
    if ($display_read_later === 'auto' && $in_bookmarks) {
        $display_read_later = false;
    } elseif ($display_read_later === 'auto') {
        $display_read_later = true;
    }

    $is_read = $link->isReadBy($current_user);
    if ($display_mark_as_read === 'auto' && $is_read) {
        $display_mark_as_read = false;
    } elseif ($display_mark_as_read === 'auto') {
        $display_mark_as_read = true;
    }

    $option_compact_mode = $current_user && $current_user->option_compact_mode;
?>

<div class="link <?= $option_compact_mode ? 'link--compact' : '' ?>">
    <?php if ($display_published_at || $in_error || $trackers_detected): ?>
        <div class="link__meta">
            <?php if ($in_error || $trackers_detected): ?>
                <?php if ($is_current_user_owner): ?>
                    <button
                        class="link__meta-warning"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('repairing link', ['id' => $link->id]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                    >
                        <?php if ($in_error): ?>
                            <span title="<?= _('This link seems to be inaccessible, you should repair it.') ?>">
                                <?= icon('disconnect') ?>&nbsp;<?= _('inaccessible') ?>
                            </span>
                        <?php endif; ?>

                        <?php if ($in_error && $trackers_detected): ?>
                            <br class="no-mobile" />
                        <?php endif; ?>

                        <?php if ($trackers_detected): ?>
                            <span title="<?= _('Trackers have been detected in this link, you should repair it.') ?>">
                                <?= icon('eye') ?>&nbsp;<?= _('trackers') ?>
                            </span>
                        <?php endif; ?>
                    </button>
                <?php else: ?>
                    <span class="link__meta-warning">
                        <?php if ($in_error): ?>
                            <span title="<?= _('This link seems to be inaccessible, you may have difficulties to access it.') ?>">
                                <?= icon('disconnect') ?> <?= _('inaccessible') ?>
                            </span>
                        <?php endif; ?>

                        <?php if ($in_error && $trackers_detected): ?>
                            <br />
                        <?php endif; ?>

                        <?php if ($trackers_detected): ?>
                            <span title="<?= _('Trackers have been detected in this link, your privacy might not be respected if you click on it.') ?>">
                                <?= icon('eye') ?> <?= _('trackers') ?>
                            </span>
                        <?php endif; ?>
                    </span>
                <?php endif; ?>
            <?php endif; ?>

            <?php if ($display_published_at): ?>
                <div class="link__meta-separator"></div>

                <time class="link__meta-date" datetime="<?= $link->published_at->format(DATE_ATOM) ?>" title="<?= _date($link->published_at, 'dd MMM Y, HH:mm') ?>">
                    <?php if ($now->format('Y') === $link->published_at->format('Y')): ?>
                        <?= _date($link->published_at, 'dd MMM') ?>
                    <?php else: ?>
                        <?= _date($link->published_at, 'dd MMM Y') ?>
                    <?php endif; ?>
                </time>
            <?php endif; ?>
        </div>
    <?php endif; ?>

    <div class="link__body">
        <a
            class="link__image-container anchor--hidden"
            target="_blank"
            rel="noopener noreferrer"
            tabindex="-1"
            href="<?= protect($link->url) ?>"
        >
            <img class="link__image" alt="<?= protect($link->title) ?>" src="<?= url_media('cards', $link->image_filename) ?>" loading="lazy" />
        </a>

        <div class="link__content">
            <h2 class="link__title">
                <a
                    target="_blank"
                    rel="noopener noreferrer"
                    href="<?= protect($link->url) ?>"
                >
                    <?= protect($link->title) ?>
                </a>
            </h2>

            <p class="link__text link__text--oneline">
                <?php if (!$link->fetched_at): ?>
                    <span class="link__ellipsis">
                        <?= icon('sync', 'spin') ?>
                        <?= _('ongoing synchronisation…') ?>
                    </span>
                <?php else: ?>
                    <span class="link__ellipsis"><?= protect($link->host()) ?></span>&nbsp;·&nbsp;<?= format_reading_time($link->reading_time) ?>
                <?php endif; ?>

                <?php if ($is_read): ?>
                    &nbsp;<span title="<?= _('You read this link.') ?>"><?= icon('check') ?><span class="sr-only"><?= _('You read this link.') ?></span></span>
                <?php endif; ?>

                <?php if ($in_bookmarks): ?>
                    &nbsp;<span title="<?= _('This link is in your bookmarks.') ?>"><?= icon('bookmark') ?><span class="sr-only"><?= _('This link is in your bookmarks.') ?></span></span>
                <?php endif; ?>
            </p>

            <?php if ($display_source): ?>
                <p class="link__text news__source">
                    <?php if ($link->source_type === 'bookmarks'): ?>
                        <?= _f('via your <strong><a class="anchor--hidden" href="%s">bookmarks</a></strong>', url('bookmarks')) ?>
                    <?php elseif ($source_collection): ?>
                        <?php if ($source_collection->type === 'collection'): ?>
                            <?php $owner = $source_collection->owner(); ?>
                            <?= _f(
                                'via <strong><a class="anchor--hidden" href="%s">%s</a></strong> by <a class="anchor--hidden" href="%s">%s</a>',
                                url('collection', ['id' => $source_collection->id]),
                                protect($source_collection->name()),
                                url('profile', ['id' => $owner->id]),
                                protect($owner->username)
                            ) ?>
                        <?php else: ?>
                            <?= _f(
                                'via <strong><a class="anchor--hidden" href="%s">%s</a></strong>',
                                url('collection', ['id' => $source_collection->id]),
                                protect($source_collection->name())
                            ) ?>
                        <?php endif; ?>
                    <?php elseif ($source_user): ?>
                        <?= _f(
                            'via <strong><a class="anchor--hidden" href="%s">%s</a></strong>',
                            url('profile', ['id' => $source_user->id]),
                            protect($source_user->username)
                        ) ?>
                    <?php endif; ?>
                </p>
            <?php endif; ?>

            <?php if ($link->tags): ?>
                <div class="link__tags">
                    <?php foreach ($link->tags as $tag): ?>
                        <a class="tag" href="<?= url('links', ['q' => "#{$tag}"]) ?>">
                            #<?= $tag ?>
                        </a>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>

            <?php if ($display_notes): ?>
                <p class="link__text">
                    <a href="<?= url('link', ['id' => $link->id]) ?>">
                        <?= icon('note') ?>
                        <?php if ($link->number_notes > 0): ?>
                            <?= _f('Notepad (%s)', format_number($link->number_notes)) ?>
                        <?php else: ?>
                            <?= _('Notepad') ?>
                        <?php endif ?>
                    </a>

                    <?php if ($display_hidden): ?>
                        <span class="sticker sticker--right">
                            <?= _('hidden') ?>
                        </span>
                    <?php endif; ?>
                </p>
            <?php endif; ?>

            <?php if ($display_external_comments): ?>
                <p class="link__text">
                    <a
                        target="_blank"
                        rel="noopener noreferrer"
                        href="<?= protect($link->url_replies) ?>"
                    >
                        <?= icon('comment') ?>
                        <?= _('Comments') ?>
                    </a>
                </p>
            <?php endif; ?>
        </div>
    </div>

    <div class="link__actions">
        <details
            class="popup"
            data-controller="popup"
            data-action="toggle->popup#update click@window->popup#closeOnClickOutside keydown->popup#closeOnEscape"
        >
            <summary class="popup__opener" title="<?= _('Actions') ?>">
                <span class="button button--icon button--ghost button--smaller">
                    <?= icon('menu') ?>
                    <span class="sr-only">
                        <?= _('Actions') ?>
                    </span>
                </span>
            </summary>

            <nav class="popup__container popup__container--over" role="menu">
                <?php if ($display_never): ?>
                    <form data-turbo-preserve-scroll method="post" action="<?= url('mark link to never read', ['id' => $link->id]) ?>" role="menuitem">
                        <button class="popup__item popup__item--button">
                            <?= icon('times') ?>
                            <?= _('Remove from the news') ?>
                        </button>

                        <input type="hidden" name="csrf_token" value="<?= csrf_token('links\\MarkLinkAsNever') ?>" />
                    </form>

                    <div class="popup__separator"></div>
                <?php endif; ?>

                <div data-controller="copy-to-clipboard">
                    <input type="hidden" value="<?= protect($link->url) ?>" data-copy-to-clipboard-target="copyable" />

                    <button
                        class="popup__item popup__item--button share__button"
                        data-action="copy-to-clipboard#copy"
                        data-copy-to-clipboard-target="feedback"
                        role="menuitem"
                    >
                        <?= icon('copy-to-clipboard') ?>
                        <?= _('Copy the external link') ?>
                    </button>
                </div>

                <?php if ($display_edit || $display_repair): ?>
                    <div class="popup__separator"></div>
                <?php endif; ?>

                <?php if ($display_edit): ?>
                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('edit link', ['id' => $link->id]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('pencil') ?>
                        <?= _('Edit') ?>
                    </button>
                <?php endif; ?>

                <?php if ($display_repair): ?>
                    <button
                        class="popup__item popup__item--button"
                        data-controller="modal-opener"
                        data-action="modal-opener#fetch"
                        data-modal-opener-href-value="<?= url('repairing link', ['id' => $link->id]) ?>"
                        aria-haspopup="dialog"
                        aria-controls="modal"
                        role="menuitem"
                    >
                        <?= icon('wrench') ?>
                        <?= _('Repair') ?>
                    </button>
                <?php endif; ?>

                <?php if ($display_delete): ?>
                    <div class="popup__separator"></div>

                    <form
                        data-turbo-preserve-scroll
                        method="post"
                        action="<?= url('delete link', ['id' => $link->id]) ?>"
                        data-turbo-confirm="<?= _('Are you sure that you want to delete this link?') ?>"
                    >
                        <button
                            type="submit"
                            class="popup__item popup__item--button"
                            role="menuitem"
                        >
                            <?= icon('trash') ?>
                            <?= _('Delete') ?>
                        </button>

                        <input type="hidden" name="csrf_token" value="<?= csrf_token('links\\DeleteLink') ?>" />
                    </form>
                <?php endif; ?>
            </nav>
        </details>

        <div class="link__actions-group">
            <?php if ($display_mark_as_read): ?>
                <form data-turbo-preserve-scroll method="post" action="<?= url('mark link as read', ['id' => $link->id]) ?>">
                    <button class="button--icon button--ghost button--smaller" title="<?= _('Mark as read') ?>">
                        <?= icon('check') ?>
                        <span class="sr-only">
                            <?= _('Mark as read') ?>
                        </span>
                    </button>

                    <input type="hidden" name="csrf_token" value="<?= csrf_token('links\\MarkLinkAsRead') ?>" />
                </form>
            <?php endif; ?>

            <?php if ($display_mark_as_unread): ?>
                <form data-turbo-preserve-scroll method="post" action="<?= url('mark link as unread', ['id' => $link->id]) ?>">
                    <button class="button--icon button--ghost button--smaller" title="<?= _('Remove from read list') ?>">
                        <?= icon('uncheck') ?>
                        <span class="sr-only">
                            <?= _('Remove from read list') ?>
                        </span>
                    </button>

                    <input type="hidden" name="csrf_token" value="<?= csrf_token('links\\MarkLinkAsUnread') ?>" />
                </form>
            <?php endif; ?>

            <?php if ($display_read_later): ?>
                <form data-turbo-preserve-scroll method="post" action="<?= url('read link later', ['id' => $link->id]) ?>">
                    <button class="button--icon button--ghost button--smaller" title="<?= _('Read later') ?>">
                        <?= icon('bookmark') ?>
                        <span class="sr-only">
                            <?= _('Read later') ?>
                        </span>
                    </button>

                    <input type="hidden" name="csrf_token" value="<?= csrf_token('links\\MarkLinkAsReadLater') ?>" />
                </form>
            <?php endif; ?>

            <button
                class="button--icon button--ghost button--smaller"
                data-controller="modal-opener"
                data-action="modal-opener#fetch"
                data-modal-opener-href-value="<?= url('link collections', ['id' => $link->id, 'mark_as_read' => $storing_must_mark_as_read]) ?>"
                aria-haspopup="dialog"
                aria-controls="modal"
                title="<?= _('Store the link') ?>"
            >
                <?= icon('collection') ?>
                <span class="sr-only">
                    <?= _('Store the link') ?>
                </span>
            </button>
        </div>
    </div>
</div>
